home *** CD-ROM | disk | FTP | other *** search
/ Light ROM 1 / LIGHT-ROM 1 (Amiga Library Services)(1994).iso / ffdisks / d939.lha / ExtraCmds / source_etc.lha / src / TimeCom.c < prev    next >
C/C++ Source or Header  |  1993-10-22  |  4KB  |  138 lines

  1. /*   ---------------------------------      -------     
  2.  *   |\  | | | | |  |.| |   \|  |/ /|\      |||||||     
  3.  *   | | | |/  | |\ |/  |/|  |\ |/  |    ?  ---+---  =< 
  4.  *   | | | |   | |  |     |  |  |   |     \qqqqqqqqq/   
  5.  *   ---------------------------------  ~~~~~~~~~~~~~~~~
  6.  *  TimeCom - Time a command.
  7.  *  Copyright (C) 1992, 1993 Torsten Poulin
  8.  *
  9.  *  This program is free software; you can redistribute it and/or modify
  10.  *  it under the terms of the GNU General Public License as published by
  11.  *  the Free Software Foundation; either version 2 of the License, or
  12.  *  (at your option) any later version.
  13.  *
  14.  *  This program is distributed in the hope that it will be useful,
  15.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  *  GNU General Public License for more details.
  18.  *
  19.  *  You should have received a copy of the GNU General Public License
  20.  *  along with this program; if not, write to the Free Software
  21.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  *
  23.  *  The author can be contacted by s-mail at
  24.  *    Torsten Poulin
  25.  *    Banebrinken 99, 2, 77
  26.  *    DK-2400 Copenhagen NV
  27.  *    DENMARK
  28.  *
  29.  * Created 16-Jan-92
  30.  * $Id: TimeCom.c,v 37.2 93/02/13 21:42:07 Torsten Rel $
  31.  * $Log:    TimeCom.c,v $
  32.  * Revision 37.2  93/02/13  21:42:07  Torsten
  33.  * Changed the version tag and added the copyright ditto.
  34.  * 
  35.  */
  36.  
  37. #include <exec/types.h>
  38. #include <libraries/dos.h>
  39. #include <dos/dos.h>
  40. #include <dos/dostags.h>
  41. #include <proto/exec.h>
  42. #include <proto/dos.h>
  43. #include <proto/intuition.h>
  44. #include "timecom_rev.h"
  45.  
  46. #define OPT_TO      0
  47. #define OPT_APPEND  1
  48. #define OPT_COMMAND 2
  49.  
  50.  
  51. static LONG MySystemTags(struct DosLibrary *DOSBase,
  52.                          UBYTE *command, ULONG firsttag, ...);
  53.  
  54. char const versionID[] = VERSTAG;
  55. char const copyright[] = "$COPYRIGHT:© 1992,1993 Torsten Poulin$";
  56.  
  57. LONG entrypoint(VOID)
  58. {
  59.   struct RDArgs        *args;
  60.  
  61.   struct IntuitionBase *IntuitionBase;
  62.   struct DosLibrary    *DOSBase;
  63.   struct Library       *SysBase;
  64.  
  65.   BPTR  out;
  66.   ULONG secs1 = 0, secs2 = 0, not_used = 0;
  67.   LONG  arg[3];
  68.   LONG  rc = RETURN_OK;
  69.  
  70.  
  71.   SysBase = *(struct Library **) 4L;
  72.   if (!(DOSBase = (struct DosLibrary *) OpenLibrary("dos.library", 37L)))
  73.   {
  74.     rc = RETURN_FAIL;
  75.     goto exit1;
  76.   }
  77.   if (!(IntuitionBase = (struct IntuitionBase *)
  78.     OpenLibrary("intuition.library", 33L)))
  79.   {
  80.     rc = RETURN_FAIL;
  81.     goto exit2;
  82.   }
  83.  
  84.   arg[OPT_TO] = arg[OPT_APPEND] = arg[OPT_COMMAND] = 0L;
  85.     
  86.   if (args = ReadArgs("TO/K,APPEND/S,COMMAND/F/A", arg, NULL))
  87.   {
  88.     CurrentTime(&secs1, ¬_used);
  89.     rc = MySystemTags(DOSBase, (UBYTE *) arg[OPT_COMMAND],
  90.               SYS_UserShell, TRUE,
  91.               TAG_DONE);
  92.     CurrentTime(&secs2, ¬_used);
  93.     if (rc == -1)
  94.       rc = RETURN_ERROR;
  95.     secs2 -= secs1;
  96.  
  97.     if (out = Open(arg[OPT_TO] ? (UBYTE *) arg[OPT_TO] : "CONSOLE:",
  98.            (BOOL) arg[OPT_APPEND] ? MODE_READWRITE : MODE_NEWFILE))
  99.     {
  100.       if ((BOOL) arg[OPT_APPEND] && arg[OPT_TO])
  101.     Seek(out, 0L, OFFSET_END);
  102.       VFPrintf(out, "Elapsed time: %ld second", (LONG *) &secs2);
  103.       FPuts(out, secs2 == 1 ? "." : "s.");
  104.       if (arg[OPT_TO])
  105.     VFPrintf(out, " Command: %s", (LONG *) &arg[OPT_COMMAND]);
  106.       FPutC(out, '\n');
  107.       Close(out);
  108.     }
  109.     else
  110.     {
  111.       LONG err = IoErr();
  112.       PrintFault(err, "TimeCom");
  113.       rc = RETURN_ERROR;
  114.     }
  115.  
  116.     FreeArgs(args);
  117.   }
  118.   else
  119.   {
  120.     LONG err = IoErr();
  121.     PrintFault(err, "TimeCom");
  122.     rc = RETURN_ERROR;
  123.   }
  124.  
  125.   CloseLibrary((struct Library *) IntuitionBase);
  126.  exit2:
  127.   CloseLibrary((struct Library *) DOSBase);
  128.  exit1:
  129.   return rc;
  130. }
  131.  
  132.  
  133. static LONG MySystemTags(struct DosLibrary *DOSBase,
  134.                          UBYTE *command, ULONG firsttag, ...)
  135. {
  136.   return SystemTagList(command, (struct TagItem *) &firsttag);
  137. }
  138.